function Node(data){
    this.data = data;
    this.parent = null;
    this.children = [];
}
function Tree(data){
   this._root = new Node(data);
}
Tree.prototype = {
    traverseDF:function(callback){
      (function recurse (current){
        for(let i=0;i<current.children.length;i++){
            recurse(current.children[i]);
        }
        callback(current);
      })(this._root);
    },
    traverseBF:function(callback){
       let queue = new Queue();
       queue.enqueue(this._root);
       let currentTree = queue.dequeue();
       while(currentTree){
           for(let i=0;i<currentTree.children.length;i++){
            queue.enqueue(currentTree.children[i]);
           }
           callback(currentTree);
           currentTree = queue.dequeue();
       }
    },
    contains:function(callback,tools){
       tools.call(this,callback);
    },
    add:function(data,nodeRoot,tools){
        let node = new Node(data);
        let nodeParent = null;//
        let addVal = function(val){
           if(val.data == nodeRoot){
            //   可以放到内部添加
            //   node.parent = val.data; 
            //   val.children.push(node);
              nodeParent = val;
           }
        }
        //call 传参数  apply传数组  都可以 
        tools.apply(this,[addVal]);
        // tools.call(this,addVal);
        // 放到递归函数的外部 可以判断父级是否合法 nodeParent 是引用类型 就是个指针地址其实还是 val
        if(nodeParent){
            // 这样添加parent是父对象，但是parent无限循环这样真的好么
            node.parent = nodeParent;
            // node.parent = nodeParent.data;
            nodeParent.children.push(node);
        }else{
            alert('父级不合法');
        }

    },
    remove:function(nodeName,tools){
        let parent = null;
        let child = null;
        let callback = function(val){
            if(val.data == nodeName){
               parent = val.parent;
               child = val;
            }
        }
        tools.call(this,callback);
        if(parent){
           let index = findIndex(parent,child);
           console.log(index);
           parent.children.splice(index,1);
        }
    },
    render:function(id){
        let container = document.querySelector(id);
        let html = '';
        (function getHtml(node){
           html += `<div>${node.data}${index}</div>`;
           if(node.children.length>0){
             console.log(index);
             for(let i=0;i<node.children.length;i++){
              getHtml(node.children[i]);
             }
           }
        })(this._root)
        console.log(html);
        container.innerHTML = html;
    },
    init:function(id){
        this.render(id);
    }
}
function findIndex(parArr,chdArr){
   let index = null;
   for(let i = 0;i<parArr.children.length;i++){
      if(parArr.children[i].data == chdArr.data){
        index = i;
        break;
      }
   }
   return index;
}
let tree = new Tree('ceo');
tree.add('cfo','ceo',tree.traverseDF);
tree.add('coo','ceo',tree.traverseDF);
tree.add('cxo','coo',tree.traverseDF);
function b(val){
  // console.log(val);
}
tree.traverseBF(b);
tree.init('#container');
// console.log(tree);